package com.mjf.cjt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mjf.cjt.domain.MjfManufactureOrder;
import com.mjf.cjt.vo.MjfManufactureOrderVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 生成加工单 Mapper 接口
 * </p>
 *
 * @author
 * @since 2025-04-05
 */
@Mapper
public interface MjfManufactureOrderMapper extends BaseMapper<MjfManufactureOrder> {

    /**
     * 查询加工单及其明细
     *
     * @param code 加工单号
     * @param reviser 业务员
     * @param inventoryName 产品名称
     * @param detailsSaleOrderCode 销售订单号
     * @return 加工单及其明细
     */
    @Select("<script>" +
            "SELECT " +
            "mo.id, mo.code, mo.ts, mo.status, mo.dtoclassname, mo.customer, mo.voucherdate, " +
            "mo.busitypeid, mo.busitypecode, mo.busitypename, mo.departmentid, mo.departmentcode, " +
            "mo.departmentname, mo.person, mo.project, mo.prestartdate, mo.prefinishdate, " +
            "mo.startdate, mo.finishdate, mo.saleordercode, mo.isproductautoaddmaterial, " +
            "mo.voucherstateid, mo.voucherstatecode, mo.voucherstatename, mo.qualityinspectionstateid, " +
            "mo.qualityinspectionstatecode, mo.qualityinspectionstatename, mo.memo, mo.maker, " +
            "mo.reviser, mo.auditor, mo.auditeddate, mo.printcount, mo.changer, mo.changedate, " +
            "md.id AS detailsId, md.m_id AS mId, md.ts AS detailsTs, md.status AS detailsStatus, " +
            "md.haspra AS hasPra, md.inventorybarcode AS inventoryBarcode, " +
            "md.project AS detailsProject, md.inventoryid AS inventoryId, " +
            "md.inventorycode AS inventoryCode, md.inventoryname AS inventoryName, " +
            "md.inventoryimagefile AS inventoryImageFile, md.inventoryspecification AS inventorySpecification, " +
            "md.inventorypriuserdefnvc1 AS inventoryPriUserDefNvc1, md.inventorypriuserdefnvc2 AS inventoryPriUserDefNvc2, " +
            "md.inventorypriuserdefnvc3 AS inventoryPriUserDefNvc3, md.inventorypriuserdefnvc4 AS inventoryPriUserDefNvc4, " +
            "md.inventorypriuserdefnvc5 AS inventoryPriUserDefNvc5, md.inventorypriuserdefnvc6 AS inventoryPriUserDefNvc6, " +
            "md.inventorypriuserdefnvc7 AS inventoryPriUserDefNvc7, md.inventorypriuserdefdecm1 AS inventoryPriUserDefDecm1, " +
            "md.inventorypriuserdefdecm2 AS inventoryPriUserDefDecm2, md.inventorypriuserdefdecm3 AS inventoryPriUserDefDecm3, " +
            "md.inventorypriuserdefdecm4 AS inventoryPriUserDefDecm4, md.inventorypriuserdefdecm5 AS inventoryPriUserDefDecm5, " +
            "md.inventorypriuserdefdecm6 AS inventoryPriUserDefDecm6, md.bomid AS bomId, " +
            "md.bomcode AS bomCode, md.bomname AS bomName, md.routing AS routing, " +
            "md.blueprint AS blueprint, md.techrequirements AS techRequirements, " +
            "md.bomexpandtypeid AS bomExpandTypeId, md.bomexpandtypecode AS bomExpandTypeCode, " +
            "md.bomexpandtypename AS bomExpandTypeName, md.warehouse AS warehouse, " +
            "md.batch AS batch, md.unitid AS unitId, md.unitcode AS unitCode, " +
            "md.unitname AS unitName, md.quantity AS quantity, md.unitexchangerate AS unitExchangeRate, " +
            "md.unit2 AS unit2, md.quantity2 AS quantity2, md.compositionquantity AS compositionQuantity, " +
            "md.prestartdate AS detailsPreStartDate, md.prefinishdate AS detailsPreFinishDate, " +
            "md.startdate AS detailsStartDate, md.finishdate AS detailsFinishDate, " +
            "md.ismanualfinish AS isManualFinish, md.isneedqualityinspection AS isNeedQualityInspection, " +
            "md.inspecttype AS inspectType, md.inspectionsamplingrate AS inspectionSamplingRate, " +
            "md.wholesuittotalreportquantity AS wholeSuitTotalReportQuantity, " +
            "md.totalinspectionquantity AS totalInspectionQuantity, " +
            "md.wholesuitmaterialquantity AS wholeSuitMaterialQuantity, " +
            "md.totalstockinquantity AS totalStockInQuantity, " +
            "md.totalinspectionquantity2 AS totalInspectionQuantity2, " +
            "md.totalstockinquantity2 AS totalStockInQuantity2, " +
            "md.sourcevouchertype AS sourceVoucherType, md.sourcevouchercode AS sourceVoucherCode, " +
            "md.saleordercode AS detailsSaleOrderCode, md.customer AS detailsCustomer, " +
            "md.availablequantity AS availableQuantity, " +
            "md.availablecompositionquantity AS availableCompositionQuantity, " +
            "md.existingquantity AS existingQuantity, " +
            "md.existingcompositionquantity AS existingCompositionQuantity, " +
            "md.barcode AS barcode, md.generateorderstatus as generateOrderStatus, " +
            "mo.pubuserdefnvc7, mo.pubuserdefnvc8, md.pubuserdefnvc2 " +
            "FROM mjf_manufactureorder_details md " +
            "LEFT JOIN mjf_manufactureorder mo ON mo.id = md.m_id " +
            "<where>" +
            "<if test='startDateStr != null'>AND mo.voucherdate &gt;= #{startDateStr} </if>" +
            "<if test='endDateStr != null'>AND mo.voucherdate &lt;= #{endDateStr} </if>" +
            "<if test='code != null and code.trim() != \"\"'>AND mo.code LIKE CONCAT('%', #{code}, '%') </if>" +
            "<if test='reviser != null and reviser.trim() != \"\"'>AND mo.pubuserdefnvc8 LIKE CONCAT('%', #{reviser}, '%') </if>" +
            "<if test='inventoryName != null and inventoryName.trim() != \"\"'>AND md.inventoryname LIKE CONCAT('%', #{inventoryName}, '%') </if>" +
            "<if test='detailsSaleOrderCode != null and detailsSaleOrderCode.trim() != \"\"'>AND md.saleordercode LIKE CONCAT('%', #{detailsSaleOrderCode}, '%') </if>" +
            "</where>" +
            "</script>")
        List<MjfManufactureOrderVo> selectManufactureOrderWithDetails(
                @Param("startDateStr") String startDate,
                @Param("endDateStr") String endDate,
                @Param("code") String code,
                @Param("reviser") String reviser,
                @Param("inventoryName") String inventoryName,
                @Param("detailsSaleOrderCode") String detailsSaleOrderCode
        );
}
